package org.jsola.hr.helper;

import org.jsola.common.ClassKit;
import org.jsola.common.DateKit;
import org.jsola.hr.dto.EmpTrainAddDTO;
import org.jsola.hr.dto.EmpTrainUpdateDTO;
import org.jsola.hr.entity.EmpTrainDO;
import org.jsola.hr.query.EmpTrainQuery;
import org.jsola.hr.service.IEmpTrainService;
import org.jsola.hr.vo.EmpTrainVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

import static org.jsola.hr.core.AbstractTest.SITE_ID;



/**
 * 员工培训记录
 *
 * @author zhr
 */
@Component
public class EmpTrainTestHelper extends BaseTestHelper {
    
    @Autowired
    private IEmpTrainService empTrainService;

    /**
     * 生成add测试数据
     * @return EmpTrainAddDTO
     */
    public EmpTrainAddDTO getEmpTrainAddDTO(){
        EmpTrainAddDTO empTrainAddDTO = new EmpTrainAddDTO();
        empTrainAddDTO.setEmpId(getRandomValue("varchar",32));
        empTrainAddDTO.setTrainCourse(getRandomValue("varchar",255));
        empTrainAddDTO.setStartTime((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        empTrainAddDTO.setEndTime((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        empTrainAddDTO.setTrainDuration(getRandomValue("varchar",255));
        empTrainAddDTO.setTrainOrgName(getRandomValue("varchar",255));
        empTrainAddDTO.setAssessResult(getRandomValue("varchar",255));
        empTrainAddDTO.setTrainAddress(getRandomValue("varchar",255));
        empTrainAddDTO.setTrainMode(getRandomValue("varchar",255));
        empTrainAddDTO.setTrainCerName(getRandomValue("varchar",255));
        empTrainAddDTO.setTrainCerNumber(getRandomValue("varchar",255));
        empTrainAddDTO.setRemake(getRandomValue("text",2147483647));
        empTrainAddDTO.setCompanyId(getRandomValue("varchar",32));
        return empTrainAddDTO;
    }


    /**
     * 生成update测试数据
     * @return EmpTrainUpdateDTO
     */
    public EmpTrainUpdateDTO getEmpTrainUpdateDTO(EmpTrainVO empTrainVO){
        return empTrainService.selectDOById(empTrainVO.getId(), SITE_ID).to(EmpTrainUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return EmpTrainQuery
     */
    public EmpTrainQuery getEmpTrainQuery() {
        EmpTrainQuery empTrainQuery = new EmpTrainQuery();
        List<EmpTrainDO> empTrainDOList = empTrainService.list(new EmpTrainDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(empTrainDOList)) {
            EmpTrainDO empTrainDO = empTrainDOList.get(0);
            empTrainQuery = empTrainDO.to(EmpTrainQuery.class);
            if (empTrainDO.getStartTime() != null) {
                empTrainQuery.setStartTimeStart(DateKit.getBeforeDay(empTrainDO.getStartTime(),-1));
                empTrainQuery.setStartTimeEnd(DateKit.getBeforeDay(empTrainDO.getStartTime(),1));
            }
            if (empTrainDO.getEndTime() != null) {
                empTrainQuery.setEndTimeStart(DateKit.getBeforeDay(empTrainDO.getEndTime(),-1));
                empTrainQuery.setEndTimeEnd(DateKit.getBeforeDay(empTrainDO.getEndTime(),1));
            }
            if (empTrainDO.getGmtCreate() != null) {
                empTrainQuery.setGmtCreateStart(DateKit.getBeforeDay(empTrainDO.getGmtCreate(),-1));
                empTrainQuery.setGmtCreateEnd(DateKit.getBeforeDay(empTrainDO.getGmtCreate(),1));
            }
            if (empTrainDO.getGmtModified() != null) {
                empTrainQuery.setGmtModifiedStart(DateKit.getBeforeDay(empTrainDO.getGmtModified(),-1));
                empTrainQuery.setGmtModifiedEnd(DateKit.getBeforeDay(empTrainDO.getGmtModified(),1));
            }
        }
        return empTrainQuery;
    }

    /**
     * 保存示例
     * @return EmpTrainVO
     */
    public EmpTrainVO saveEmpTrainExample(){
        EmpTrainAddDTO addDTO = this.getEmpTrainAddDTO();
        EmpTrainVO empTrainVO = empTrainService.save(addDTO,buildTokenUser());
        return empTrainVO;
    }


}

